

<link rel="stylesheet" href="/css/welcome.css" />
<link rel="stylesheet" href="/css/percircle.css" />

<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main welcome">
        <div class="layui-row layui-col-space15">
            <div class="layui-row" id="app">
                <div class="layui-col-md12">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-md4">
                            <div class="layui-card">
                                <div class="layui-card-header"><i class="fa fa-file-text icon"></i>任务总览</div>
                                <div class="layui-card-body">
                                    <div class="welcome-module">
                                        <div class="layui-row layui-col-space10">
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-blue">实时</span>
                                                            <h5 layui-bg-blue>总任数目</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:blue">{{job.total}}</h1>
                                                            <small>当天的任务</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-green">实时</span>
                                                            <h5>执行中任务</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:green">{{job.process}}</h1>
                                                            <small>接料前、送料中的</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-orange">实时</span>
                                                            <h5>已完成任务</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:orange">{{job.complete}}</h1>
                                                            <small>正常完成的</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-red">实时</span>
                                                            <h5>异常任务</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:red">{{job.cancel}}</h1>
                                                            <small>取消、打断等</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md4">
                            <div class="layui-card">
                                <div class="layui-card-header"><i class="fa fa-tags icon"></i>AGV信息</div>
                                <div class="layui-card-body">
                                    <div class="welcome-module" id="robotInfo">

                                        <ul class="robot">
                                            <li><span class="title">IP:</span><span class="content">{{robot.ip}}</span></li>
                                            <li><span class="title">Port:</span><span class="content">{{robot.port}}</span></li>
                                            <li><span class="title">Mode:</span><span class="content">{{robot.mode}}</span></li>
                                            <li><span class="title">Status:</span><span class="content">{{robot.status}}</span></li>
                                            <li><span class="title">Connect:</span><span class="content">{{robot.isConnected}}</span></li>
                                            <li><span class="title">IsAvailable:</span><span class="content">{{robot.isAvailable}}</span></li>
                                            <li>
                                                <span class="title">X:</span><span class="content">{{robot.posX}}</span>
                                                <span class="title">Y:</span><span class="content">{{robot.posY}}</span>
                                                <span class="title">Z:</span><span class="content">{{robot.posTh}}</span>
                                            </li>
                                        </ul>
                                        <h2 id="robotName" class="robot">{{robot.name}}</h2>
                                        <div id="orangecircle" class="c100 p60 green">
                                            <span class="robot" style="color:green">{{robot.bettery}}</span>
                                            <div class="slice">
                                                <div class="bar"></div>
                                                <div class="fill"></div>
                                            </div>
                                        </div>
                                        <div id="leftArrow" @click="Add"></div>
                                        <div id="rightArrow" @click="Decrease"></div>


                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md4">

                            <div class="layui-card">
                                <div class="layui-card-header"><i class="fa fa-tags icon"></i>设备信息</div>
                                <div class="layui-card-body">
                                    <div class="welcome-module" id="deviceInfo">
                                        <h2 id="deviceName" class="device" align="center">{{device.portName}}</h2>
                                        <ul class="device" style="display:inline-block;text-align:center;width:100%">

                                            <li><span class="title">Connect:</span><span class="content">{{device.isConnected}}</span></li>
                                            <li><span class="title">IP:</span><span class="content">{{device.ip}}</span></li>
                                            <li><span class="title">PlcAddress:</span><span class="content">{{device.plcAddress}}</span></li>
                                            <li><span class="title">PlcValue:</span><span class="content">{{device.plcValue}}</span></li>
                                            <li><span class="title">AgvAddress:</span><span class="content">{{device.agvAddress}}</span></li>
                                            <li><span class="title">AgvValue:</span><span class="content">{{device.agvValue}}</span></li>

                                        </ul>

                                        <div id="leftArrow" @click="DAdd"></div>
                                        <div id="rightArrow" @click="DDecrease"></div>


                                    </div>
                                </div>
                            </div>



                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">

                <div class="layui-col-md4" style="padding-right:7.5px">

                    <div class="layui-card">
                        <div class="layui-card-header"><i class="fa fa-tags icon"></i>AGV信息统计</div>
                        <div class="layui-card-body">
                            <div class="welcome-module" id="robotPie" style="min-height:260px">

                            </div>
                        </div>
                    </div>



                </div>
                <div class="layui-col-md8">
                    <div class="layui-card">
                        <div class="layui-card-header"><i class="fa fa-line-chart icon"></i>实时任务</div>
                        <div class="layui-card-body" style="padding:0px">
                            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script type="text/html" id="stateBar0">
    <div>
        {{# if(d.taskState=== 0){ }}
        未执行
        {{# }else if(d.taskState=== 1){ }}
        前往接料
        {{# }else if(d.taskState=== 2){ }}
        接料中
        {{# }else if(d.taskState=== 3){ }}
        前往送料
        {{# }else if(d.taskState=== 4){ }}
        送料中
        {{# }else if(d.taskState=== 5){ }}
        已完成
        {{# }else if(d.taskState=== 6){ }}
        被打断
        {{# }else if(d.taskState=== 7){ }}
        被取消
        {{# }else{ }}
        未知
        {{# } }}
    </div>
</script>
<script src="../js/signalr/dist/browser/signalr.min.js"></script>
<script src="../js/vue.min.js"></script>

<script>
    layui.use(['form', 'table', 'element', 'echarts','tablePlug'], function () {
      
        var $ = layui.jquery,
            echarts = layui.echarts,
            table = layui.table;
        var tablePlug = layui.tablePlug;
        tablePlug.smartReload.enable(true);//处理不闪动的关键代码
        //渲染表格
        var tableIns = table.render({
            elem: '#currentTableId',
            url: 'api/Task/GetRealTask',
            smartReloadModel: true,
            parseData: function (res) { //res 即为原始返回的数据
                return {

                    "code": res.code === 200 ? 0 : res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.result, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            cols: [[
                { field: 'mission', width: 110, title: '任务编号', sort: true, align: "center" },
                { field: 'robotId', width: 100, title: '分配机器人', align: "center" },
                { field: 'source', width: 90, title: '接料点', align: "center" },
                { field: 'targets', width: 90, title: '目标点', align: "center" },
                { field: 'taskState', width: 90, title: '任务状态', align: "center", templet: "#stateBar0" },
                { field: 'createTime', width: 120, title: '创建时间', sort: true, align: "center", templet: "<div>{{layui.util.toDateString(d.createTime, 'HH:mm:ss')}}</div>" },
                { field: 'updateTime', width: 120, title: '更新时间', sort: true, align: "center", templet: "<div>{{layui.util.toDateString(d.updateTime, 'HH:mm:ss')}}</div>" },
                { field: 'remark', minWidth: 80, title: '备注', align: "center" },
            ]],
            limit: 15,
            page: {
                layout: ['prev', 'page', 'next', 'skip', 'count', 'limit', 'refresh']
            },
            id: 'realtable',
            even: "true",
            loading: false,
            skin: 'row',
           
      
        });

        var time = setInterval(function () {
            
                tableIns.reload({ page: { curr: 1 } });
            
        }, 5000);
       



        //Vue
        var vm = new Vue({
            el: "#app",
            mounted() {
                this.robottimer = setInterval(() => {
                    if (this.robots.length > 0) {
                        if (this.count < this.robots.length - 1)
                            this.count++;
                        else
                            this.count = 0;
                        this.robot = this.robots[this.count];
                    }



                }, 3000);
                this.devicetime = setInterval(() => {
                    if (this.devices.length > 0) {
                        if (this.dcount < this.devices.length - 1)
                            this.dcount++;
                        else
                            this.dcount = 0;
                        this.device = this.devices[this.dcount];
                    }



                }, 3000)

            },
            data: {
                robots: [],
                devices: [],
                job: {
                    total: 0,
                    cancel: 0,
                    complete: 0,
                    process: 0
                },
                count: 0,
                dcount: 0,
                robot: {
                    ip: "",
                    port: "",
                    mode: "",
                    status: "",
                    isConnected: false,
                    isAvailable: false,
                    posX: "",
                    posY: "",
                    posTh: "",
                    name: "",
                    bettery: ""
                },
                device: {
                    ip: "",
                    port: "",
                    plcAddress: "",
                    plcValue: "",
                    agvAddress: "",
                    agvValue: "",
                    isConnected: "",
                    posX: "",
                    posY: "",
                    posTh: "",
                    portName: "",

                },
                robottimer: "",
                devicetime: "",
            },
            computed: {
                charge: function () {
                    return this.robots.reduce(function (total, current) {
                        current.status.toLowerCase().indexOf("charg") >= 0 ? total++ : total;
                        return total;
                    }, 0);

                },
                offline: function () {

                    return this.robots.reduce(function (total, current) {
                        (!current.isConnected || current.status.toLowerCase().indexOf("stop") >= 0) ? total++ : total;
                        return total;
                    }, 0);

                },
                available: function () {
                    return this.robots.reduce(function (total, current) {
                        current.isAvailable ? total++ : total;
                        return total;
                    }, 0);

                },
                used: function () {
                    return this.robots.reduce(function (total, current) {
                        !(current.currentJob) ? total : total++;
                        return total;
                    }, 0);

                }
            },
            methods: {
                Add: function () {

                    if (this.count < this.robots.length - 1)
                        this.count++;
                    else
                        this.count = 0;
                    this.robot = this.robots[this.count];
                },
                Decrease: function () {

                    if (this.count <= 0)
                        this.count = this.robots.length - 1;
                    else
                        this.count--;


                    this.robot = this.robots[this.count];
                },
                DAdd: function () {

                    if (this.dcount < this.devices.length - 1)
                        this.dcount++;
                    else
                        this.dcount = 0;
                    this.device = this.device[this.dcount];
                },
                DDecrease: function () {

                    if (this.dcount <= 0)
                        this.dcount = this.devices.length - 1;
                    else
                        this.dcount--;


                    this.device = this.devices[this.dcount];
                },

            }
        })


        //SignalR
        var connection = new signalR.HubConnectionBuilder().withUrl("/reporthub").build();
        //RobotInfo
        connection.on("RobotInfo", function (robots) {
            vm.robots = robots;
            RobotChart.setOption({
                series: [{
                    type: 'pie', //饼状
                    radius: '65%', //圆的大小
                    center: ['50%', '50%'], //居中
                    label: {
                        normal: {
                            show: true,
                            formatter: '{b}: {c}'
                        }
                    },
                    data: [
                        {
                            value: vm.available,
                            name: '空闲',
                            itemStyle: {
                                color: '#1d953f'

                            }
                        },

                        {
                            value: vm.offline,
                            name: '异常',
                            itemStyle: { color: '#d71345' }
                        },

                        {
                            value: vm.used,
                            name: '占用',
                            itemStyle: {
                                color: '#fcaf17'

                            }
                        },

                        {
                            value: vm.charge,
                            name: '充电',
                            itemStyle: {
                                color: '#009ad6'

                            }
                        },
                    ]
                }]
            });
        });
        //DeviceInfo
        connection.on("DeviceInfo", function (devices) {
            vm.devices = devices;
         
        });


        //TaskInfo
        //机器人图表
        var RobotPie = {
            title: {
                text: '机器人状态统计',
                x: 'center' //标题居中
            },

            legend: {
                orient: 'vertical', //类型垂直,默认水平
                left: 'left', //类型区分在左 默认居中
                data: ['异常', '占用', '空闲', '充电']
            },
            series: [{
                type: 'pie', //饼状
                radius: '65%', //圆的大小
                center: ['50%', '50%'], //居中
                data: [{
                    value: 4,
                    name: '空闲',
                },
                {
                    value: 1,
                    name: '掉线',
                    itemStyle: { color: '#5fb878' }
                },
                {
                    value: 2,
                    name: '占用',

                },
                {
                    value: 1,
                    name: '充电',

                },
                ]
            }]
        };
        var RobotChart = echarts.init(document.getElementById('robotPie'));
        RobotChart.setOption(RobotPie, true);
        connection.on("TaskInfo", function (jobs) {

            vm.job.total = jobs.total;
            vm.job.cancel = jobs.cancel;
            vm.job.complete = jobs.complete;
            vm.job.process = jobs.process;


        });

        //处理链接关闭情况，onclose监听服务器断开和客户端主动断开  ，try catch 监听服务器无法访问等
        //在实际应用中，重新连接超过指定次数后放弃
        async function start() {
            try {
                await connection.start();
                console.log("connected");
            } catch (err) {
                console.log(err);
                setTimeout(() => start(), 5000);
            }
        };
        start();
        connection.onclose(async () => {
            console.info('监听到链接关闭');
            await start();
        });

    });



</script>
